
#include "screen_gesture.h"
#include <string.h>

typedef enum TOUCH_PULL_REGION
{
    TOUCH_PULL_REGION_TOP = 5,
    TOUCH_PULL_REGION_BOTTOM,
    TOUCH_PULL_REGION_LEFT,
    TOUCH_PULL_REGION_RIGHT,
    TOUCH_PULL_REGION_CENTER,
    TOUCH_PULL_REGION_NONE,
}TOUCH_PULL_REGION_e;

static trigger_condition_t s_trigger_condition = {0};
void lv_screen_gesture_init(trigger_condition_t *condition)
{
     if(condition)
     {
        memcpy(&s_trigger_condition, condition, sizeof(trigger_condition_t));
     }
}

//+-----TOP------+
//+--------------+
//+  -        -  +
//+  -        -  +
//+  -        -  +
//+--------------+
//+--------------+
static TOUCH_PULL_REGION_e point_region_get(int point_x, int point_y)
{
     if(point_x >= 0 && point_x <= s_trigger_condition.screen_width 
          && point_y >= 0 && point_y <= s_trigger_condition.pull_region_width)
     {
          return TOUCH_PULL_REGION_TOP;
     }
     else if(point_x >= 0 && point_x <= s_trigger_condition.pull_region_width
             && point_y >= s_trigger_condition.pull_region_width && point_y <= (s_trigger_condition.screen_height - s_trigger_condition.pull_region_width))
     {
          return TOUCH_PULL_REGION_LEFT;
     }
     else if(point_x >= 0 && point_x <= s_trigger_condition.screen_width
            && point_y >= (s_trigger_condition.screen_height - s_trigger_condition.pull_region_width) && point_y <= s_trigger_condition.screen_height)
     {
          return TOUCH_PULL_REGION_BOTTOM;
     }
     else if(point_x >= (s_trigger_condition.screen_width - s_trigger_condition.pull_region_width) && point_x < s_trigger_condition.screen_width
             && point_y >= s_trigger_condition.pull_region_width && point_y <= s_trigger_condition.screen_height)
     {
          return TOUCH_PULL_REGION_RIGHT;
     }
     else
     {
          return TOUCH_PULL_REGION_CENTER;
     }
}

void lv_screen_gesture_loop(int is_press, int point_x, int point_y)
{
     static int last_first_touch_x = 0;
     static int last_first_touch_y = 0;
     static int is_start_swip = 0;
     static TOUCH_PULL_REGION_e region_type = TOUCH_PULL_REGION_NONE;

     if(s_trigger_condition.swip_min_pixel == 0 || s_trigger_condition.cb == NULL)
     {
        return;
     }

     if(is_press == 0)
     {
         last_first_touch_x = 0;
         last_first_touch_y = 0;
         if(is_start_swip == 1 && region_type != TOUCH_PULL_REGION_CENTER)
         {
            param_info_t param = {point_x, point_y};
            s_trigger_condition.cb((SCREEN_GESTURE_TYPE_e)region_type, true, &param); 
         }
         region_type = TOUCH_PULL_REGION_NONE;
         is_start_swip = 0;
     } 
     else
     {
         if(last_first_touch_x == 0 && last_first_touch_y == 0)
         {
            last_first_touch_x = point_x;
            last_first_touch_y = point_y;
            region_type = point_region_get(point_x, point_y);
         }

         if(region_type == TOUCH_PULL_REGION_NONE)
         {
            return;
         }
         
         int x_dis = point_x - last_first_touch_x;
         int y_dis = point_y - last_first_touch_y;
         
         if((x_dis - s_trigger_condition.swip_min_pixel >= 0 
            || x_dis + s_trigger_condition.swip_min_pixel <= 0
            || y_dis - s_trigger_condition.swip_min_pixel >= 0
            || y_dis + s_trigger_condition.swip_min_pixel <= 0) && is_start_swip == 0)
         {
            is_start_swip = 1;
            //swip
            if(region_type == TOUCH_PULL_REGION_CENTER)
            {
               region_type = TOUCH_PULL_REGION_NONE; //Only trigger once per time
               if(x_dis - s_trigger_condition.swip_min_pixel >= 0)
               {
                  s_trigger_condition.cb(SCREEN_GESTURE_RIGHT_SWIP, false, NULL); 
               } 
               else if( x_dis + s_trigger_condition.swip_min_pixel <= 0)
               { 
                  s_trigger_condition.cb(SCREEN_GESTURE_LEFT_SWIP, false, NULL); 
               }
               else if(y_dis - s_trigger_condition.swip_min_pixel >= 0)
               {
                  s_trigger_condition.cb(SCREEN_GESTURE_BOTTOM_SWIP, false, NULL); 
               }
               else if(y_dis + s_trigger_condition.swip_min_pixel <= 0)
               {
                  s_trigger_condition.cb(SCREEN_GESTURE_TOP_SWIP, false, NULL); 
               }

               return ;
            }
         }
         
         //swip from edge point
         if(is_start_swip == 1)
         {
             param_info_t param = {point_x, point_y};
             s_trigger_condition.cb((SCREEN_GESTURE_TYPE_e)region_type, false, &param); 
         }

     }
}